import pandas as pd
import glob
from config import data_root
# from visualize import visualize_reg_act, visualize_age_gender_income, visualize_device_usage, visualize_purchase_categories
import os
from utils import Timer
import json
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules
from mlxtend.frequent_patterns import fpgrowth
import matplotlib.pyplot as plt
import seaborn as sns
# from preprocess import preprocess_dataframe, visualize_preprocessing_report
import numpy as np
import networkx as nx
from datetime import datetime
from itertools import combinations
from statsmodels.tsa.seasonal import seasonal_decompose
import pandas as pd
from ast import literal_eval
from handle_tasks import handle_task1, handle_task2, handle_task3, handle_task4

def process_purchase_history(row):
    # 解析purchase_history
    if isinstance(row['purchase_history'], str):
        purchase_data = json.loads(row['purchase_history'])
    else:
        purchase_data = row['purchase_history']
    
    # 收集商品信息
    items = purchase_data['items']
    product_details = []
    total_price = 0
    
    for item in items:
        product_id = item['id']
        if product_id in product_map:
            product_info = product_map[product_id]
            product_detail = {
                'id': product_id,
                'category': product_info['category'],
                'main_category': category_mapping.get(product_info['category'], '其他'),
                'price': product_info['price']
            }
            product_details.append(product_detail)
            total_price += product_info['price']
    
    # 更新purchase_history中的商品信息
    purchase_data['items'] = product_details
    purchase_data['total_price'] = total_price  # 添加实际总价
    purchase_data.pop('avg_price', None)  # 移除原有的avg_price
    purchase_data.pop('categories', None)  # 移除原有的categories字段
    
    return purchase_data

def load_data():
    parquet_files = glob.glob(os.path.join(data_root, "30G_data_new/*.parquet"))
    with Timer("读取文件") as timer:
    # 读取并合并所有文件
        ddf = pd.concat([pd.read_parquet(f) for f in parquet_files], ignore_index=True)
    print(f"{timer.name}耗时:{timer.duration}")
    return ddf

def load_data_from_jsonl(file_path='./tmp/test100.jsonl'):
    """
    从JSONL文件中读取数据并转换为DataFrame
    
    参数:
        file_path (str): JSONL文件的路径，默认为'./tmp/test100.jsonl'
    
    返回:
        pd.DataFrame: 读取并处理后的DataFrame
    """
    import pandas as pd
    import json
    from pathlib import Path
    
    # 检查文件是否存在
    if not Path(file_path).exists():
        raise FileNotFoundError(f"文件不存在: {file_path}")
    
    try:
        # 读取JSONL文件
        data = []
        with open(file_path, 'r', encoding='utf-8') as f:
            for line in f:
                data.append(json.loads(line.strip()))
        
        # 转换为DataFrame
        df = pd.DataFrame(data)
        
        print(f"成功读取数据:")
        print(f"- 记录数: {len(df)}")
        print(f"- 列数: {len(df.columns)}")
        print("\n列名:", list(df.columns))
        
        # 显示数据样例
        print("\n数据样例（第一条记录）:")
        first_record = df.iloc[0].to_dict()
        print(json.dumps(first_record, indent=2, ensure_ascii=False))
        
        return df
    
    except json.JSONDecodeError as e:
        print(f"JSON解析错误: {e}")
        raise
    except Exception as e:
        print(f"读取文件时发生错误: {e}")
        raise

if __name__ == '__main__':
    debug_mode = False
    if debug_mode:
        new_df = load_data_from_jsonl()
    else:
        print('--------------------------load and preprocess data--------------------------')
        ddf = load_data()
        with open('product_catalog.json') as f:
            jobj_list = json.load(f)['products'] # 是一个列表，形如：[{'category': '上衣', 'id': 1, 'price': 231.75},

        # 定义类别映射字典
        category_mapping = {
            '智能手机': '电子产品', '笔记本电脑': '电子产品', '平板电脑': '电子产品', 
            '智能手表': '电子产品', '耳机': '电子产品', '音响': '电子产品',
            '相机': '电子产品', '摄像机': '电子产品', '游戏机': '电子产品',
            
            '上衣': '服装', '裤子': '服装', '裙子': '服装', '内衣': '服装',
            '鞋子': '服装', '帽子': '服装', '手套': '服装', '围巾': '服装', '外套': '服装',
            
            '零食': '食品', '饮料': '食品', '调味品': '食品', '米面': '食品',
            '水产': '食品', '肉类': '食品', '蛋奶': '食品', '水果': '食品', '蔬菜': '食品',
            
            '家具': '家居', '床上用品': '家居', '厨具': '家居', '卫浴用品': '家居',
            
            '文具': '办公', '办公用品': '办公',
            
            '健身器材': '运动户外', '户外装备': '运动户外',
            
            '玩具': '玩具', '模型': '玩具', '益智玩具': '玩具',
            
            '婴儿用品': '母婴', '儿童课外读物': '母婴',
            
            '车载电子': '汽车用品', '汽车装饰': '汽车用品'
        }

        # 创建商品ID到详细信息的映射
        product_map = {item['id']: item for item in jobj_list}


        # 处理DataFrame
        new_df = ddf
        new_df['purchase_history'] = new_df.apply(lambda row: process_purchase_history(row), axis=1)

    handle_task1(new_df)
    handle_task2(new_df)
    handle_task3(new_df)
    handle_task4(new_df)

